package com.press.controllers.blog;

import com.press.bean.TaxonomyBean;
import com.press.bean.UserBean;
import com.press.beanforview.VUserBean;
import com.press.services.PersonalDataService;
import com.press.services.TaxonomyService;
import org.springframework.stereotype.Controller;
import org.springframework.test.annotation.Commit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by haocongshun on 2016/10/2.
 */
@Controller("blogUser")
@RequestMapping("/blog")
public class UserController {
    @Resource(name = "personalDataService")
    private PersonalDataService personalDataService;
    @Resource(name = "taxonomyService")
    private TaxonomyService taxonomyService;

    public TaxonomyService getTaxonomyService() {
        return taxonomyService;
    }

    public void setTaxonomyService(TaxonomyService taxonomyService) {
        this.taxonomyService = taxonomyService;
    }

    public PersonalDataService getPersonalDataService() {
        return personalDataService;
    }

    public void setPersonalDataService(PersonalDataService personalDataService) {
        this.personalDataService = personalDataService;
    }

    @RequestMapping("/user")
    public void user(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UtilController utilController = (UtilController) WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean("utilController");
        utilController.menus(request);

        getBlogOwner(request);

        Map<String ,String > option = (Map<String, String>) request.getServletContext().getAttribute("Option");
        request.setAttribute("title","欢迎访问！"+option.get("web_name")+"的博客");
        request.getRequestDispatcher("/"+option.get("web_template_id")+"/user.jsp").forward(request,response);
    }
    public void getBlogOwner(HttpServletRequest request){
        UserBean admin = personalDataService.getAdmin();
        VUserBean blogOwner = new VUserBean();
        blogOwner.setNickname(admin.getUser_nickname());
        blogOwner.setRealname(admin.getUser_realname());
        blogOwner.setEmail(admin.getUser_email());
        blogOwner.setPhone(admin.getUser_phone());
        blogOwner.setIntroduction(admin.getUser_introduction_content());
        blogOwner.setBirthday(admin.getUser_birthday());
        blogOwner.setQq(admin.getUser_qq());
        blogOwner.setWeibo(admin.getUser_weibo());
        blogOwner.setAvatar(request.getContextPath()+admin.getUser_avatar());
        blogOwner.setProfession(admin.getUser_profession());
        blogOwner.setNativePlace(admin.getUser_native_place());

        request.setAttribute("blogOwner",blogOwner);
    }
}
